package com.globalresources.business.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.globalresources.business.inface.IMessageService;
import com.globalresources.dao.inface.IFriendDao;
import com.globalresources.dao.inface.IMessageDao;
import com.globalresources.dao.inface.IMessageReceiverDao;
import com.globalresources.po.Friend;
import com.globalresources.po.Message;
import com.globalresources.po.MessageReceiver;
import com.globalresources.commons.Constant;

/**
 * @author aaron.lu(luzhiyuan) E-mail:atyuan.lu@gmail.com
 * @since 创建时间：Aug 6, 2009 3:33:59 PM
 */
public class MessageServiceImpl implements IMessageService {

	private IFriendDao friendDao;
	private IMessageDao messageDao;
	private IMessageReceiverDao messageReceiverDao;

	public IMessageDao getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(IMessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public IMessageReceiverDao getMessageReceiverDao() {
		return messageReceiverDao;
	}

	public void setMessageReceiverDao(IMessageReceiverDao messageReceiverDao) {
		this.messageReceiverDao = messageReceiverDao;
	}

	public IFriendDao getFriendDao() {
		return friendDao;
	}

	public void setFriendDao(IFriendDao friendDao) {
		this.friendDao = friendDao;
	}

	/**
	 * <p>
	 * 获取好友列表
	 * 
	 * @author aaron.lu(luzhiyuan)
	 * @param nickName
	 * @param onwerName
	 * @param isInBacklist
	 * @param pageSize
	 * @param startRow
	 * @return
	 */
	public List<Friend> getFriends(String nickName, String onwerName,
			Byte isInBacklist, int pageSize, int startRow) {
		return friendDao.getFriends(nickName, onwerName, isInBacklist,
				pageSize, startRow);
	}

	/**
	 * <P>
	 * 查询好友总纪录数
	 * 
	 * @author aaron.lu(luzhiyuan)
	 * @param onwerName
	 * @param isInBackList
	 * @return
	 */
	public Long countFriends(String onwerName, Byte isInBackList) {
		return friendDao.countFriends(onwerName, isInBackList);
	}

	/**
	 * <P>
	 * 保存或更新好友信息
	 * 
	 * @author aaron.lu(luzhiyuan)
	 * @param friend
	 */
	public void saveOrUpdateFriend(Friend friend) {
		friendDao.saveOrUpdateFriend(friend);
	}

	/**
	 * <P>
	 * 删除好友信息
	 * 
	 * @author aaron.lu(luzhiyuan)
	 * @param friend
	 */
	public void deleteFriend(Friend friend) {
		friendDao.deleteFriend(friend);
	}

	/**
	 * <P>
	 * 查询好友信息
	 * 
	 * @author aaron.lu(luzhiyuan)
	 * @param id
	 * @return
	 */
	public Friend getFriend(Integer id) {
		return friendDao.loadFriend(id);
	}

	/**
	 * <p>
	 * 保存消息
	 * 
	 * @author aaron.lu(luzhiyuan)
	 * @param message
	 */
	public void saveMessage(Message message) {
		messageDao.save(message);
	}

	/**
	 * <p>
	 * 查询消息实体
	 * 
	 * @author aaron.lu(luzhiyuan)
	 * @param username
	 * @param isDelete
	 * @param pagesize
	 * @param startrow
	 * @return
	 */
	public List<Message> getMessages(String username, Byte isDelete,
			int pagesize, int startrow) {
		return messageDao.getMessages(username, isDelete, pagesize, startrow);
	}

	/*
	 * 删除消息 <p>查询消息实体 @author songduanlei@gmail.com @param Message Object
	 * *@return
	 */
	public void deleteMessage(Message message) {
		message.setIsDelete(Constant.BOOLEAN_TRUE);
		messageDao.saveOrUpdateMessage(message);

	}

	/*
	 * 通过ID删除消息 <p>通过删除消息实体 @author songduanlei@gmail.com @param Integer Id
	 * @return boolean
	 */
	public void deleteMessageById(Integer id) {
		System.out.println("**********************************************");
		System.out.println(id);
		System.out.println("**********************************************");
		MessageReceiver messageReceiver = messageReceiverDao.find(id);
		messageReceiver.setIsDelete(Constant.BOOLEAN_TRUE);
		messageReceiverDao.saveOrUpdateMessageReceiver(messageReceiver);
	}

	/**
	 * <p>
	 * 查询消息总纪录数
	 * 
	 * @author aaron.lu(luzhiyuan)
	 * @param username
	 * @param isDelete
	 * @return
	 */
	public int countMessages(String username, Byte isDelete) {
		return messageDao.countMessages(username, isDelete);
	}

	/**
	 * <p>
	 * 查询消息接受者信息
	 * 
	 * @author aaron.lu(luzhiyuan)
	 * @param username
	 * @param isReceive
	 * @param isRead
	 * @param isDelete
	 * @param isDeleteMessage
	 * @param pageSize
	 * @param startRow
	 * @return
	 */
	public List<MessageReceiver> getMessageReceiver(String username,
			Byte isReceive, Byte isRead, Byte isDelete, Byte isDeleteMessage,
			int pageSize, int startRow) {
		return messageReceiverDao.getMessagerReceivers(username, isReceive,
				isRead, isDelete, isDeleteMessage, pageSize, startRow);
	}

	/**
	 * <p>
	 * 查询消息接受者总纪录数
	 * 
	 * @author aaron.lu(luzhiyuan)
	 * @param username
	 * @param isReceive
	 * @param isRead
	 * @param isDelete
	 * @param isDeleteMessage
	 * @return
	 */
	public int countMessageReceiver(String username, Byte isReceive,
			Byte isRead, Byte isDelete, Byte isDeleteMessage) {
		return messageReceiverDao.countMessageReceiver(username, isReceive,
				isRead, isDelete, isDeleteMessage);
	}

	/**
	 * <p>
	 * 查询消息实体
	 * 
	 * @author aaron.lu(luzhiyuan)
	 * @param id
	 * @return
	 */
	public Message getMessage(Integer id) {
		return messageDao.find(id);
	}

	/**
	 * <p>
	 * 查询消息接受者实体
	 * 
	 * @author aaron.lu(luzhiyuan)
	 * @param id
	 * @return
	 */
	public MessageReceiver getMessageReceiver(Integer id) {
		return messageReceiverDao.find(id);
	}

	/**
	 * <p>
	 * 保存消息接受者实体
	 * 
	 * @author aaron.lu(luzhiyuan)
	 * @param messageReceiver
	 */
	public void saveMessageReceiver(MessageReceiver messageReceiver) {
		messageReceiverDao.save(messageReceiver);
	}
	
	/**
	 * <p>查询匹配后的消息实体
	*@author liming.yu(yulm88@gmail.com)
	*@param keyWord 匹配关键字 
	*@param username
	*@param isDelete
	*@param pagesize
	*@param startrow
	*@return
	 */
	public List<Message> getMatchingMessages(String keyWord,String username, Byte isDelete, int pagesize, int startrow){
		return messageDao.getMatchingMessages(keyWord, username, isDelete, pagesize, startrow);
	}
	
	/**
	 * <p>查询匹配后的消息总纪录数
	*@author liming.yu(yulm88@gmail.com)
	*@param keyWord 
	*@param username
	*@param isDelete
	*@return
	 */
	public int countMatchingMessages(String keyWord,String username, Byte isDelete){
		return messageDao.countMatchingMessages(keyWord, username, isDelete);
	}
	
	/**
	 * <p>查询匹配后的消息接受者信息
	*@author liming.yu(yulm88@gmail.com)
	*@param keyWord
	*@param username
	*@param isReceive
	*@param isRead
	*@param isDelete
	*@param isDeleteMessage
	*@param pageSize
	*@param startRow
	*@return
	 */
	public List<MessageReceiver> getMatchingMessageReceiver(String keyWord,String username, Byte isReceive, Byte isRead, Byte isDelete, Byte isDeleteMessage, int pageSize, int startRow){
		return messageReceiverDao.getMatchingMessagerReceivers(keyWord, username, isReceive, isRead, isDelete, isDeleteMessage, pageSize, startRow);
	}
	
	/**
	 * <p>查询消息接受者总纪录数
	*@author liming.yu(yulm88@gmail.com)
	*@param keyWord
	*@param username
	*@param isReceive
	*@param isRead
	*@param isDelete
	*@param isDeleteMessage
	*@return
	 */
	public int countMatchingMessageReceiver(String keyWord,String username, Byte isReceive, Byte isRead, Byte isDelete, Byte isDeleteMessage){
		return messageReceiverDao.countMatchingMessageReceiver(keyWord, username, isReceive, isRead, isDelete, isDeleteMessage);
	}
	
	
}
